<template>
	<view class="login-page">
		<!-- 用户头像上传区域 -->
		<view class="avatar-section">
			<view class="avatar-container" @click="chooseImage">
				<image 
					class="avatar" 
					:src="avatarUrl || '/static/images/default-avatar.png'" 
					mode="aspectFill"
				></image>
				<!-- 只有没有头像时才显示提示文字 -->
				<view class="upload-hint" v-if="!avatarUrl">
					<text>点击上传头像</text>
				</view>
			</view>
		</view>

		<!-- 表单区域 -->
		<view class="form-section">
			<!-- 用户名输入 -->
			<view class="input-row">
				<text class="label">用户：</text>
				<input 
					class="input" 
					type="text" 
					v-model="username" 
					placeholder="输入用户名"
				/>
			</view>

			<!-- 密码输入 -->
			<view class="input-row">
				<text class="label">密码：</text>
				<input 
					class="input" 
					type="password" 
					v-model="password" 
					placeholder="输入密码"
				/>
			</view>

			<!-- 按钮组 -->
			<view class="button-row">
				<button class="btn reset-btn" @click="resetForm">重置</button>
				<button class="btn login-btn" @click="handleLogin">登录</button>
			</view>

			<!-- 注册按钮 -->
			<view class="register-row">
				<button class="register-btn" @click="goToRegister">注册</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatarUrl: '',
				username: '',
				password: ''
			};
		},
		methods: {
			// 跳转到注册页面
			goToRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				});
			},
			
			// 选择本地图片作为头像
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					sizeType: ['compressed'],
					success: (res) => {
						const tempFilePath = res.tempFilePaths[0];
						this.avatarUrl = tempFilePath;
						
						uni.showToast({
							title: '头像上传成功',
							icon: 'success',
							duration: 1500
						});
					},
					fail: (err) => {
						if (err.errMsg !== 'chooseImage:fail cancel') {
							uni.showToast({
								title: '选择图片失败',
								icon: 'none'
							});
						}
					}
				});
			},
			
			// 重置表单
			resetForm() {
				this.username = '';
				this.password = '';
				this.avatarUrl = '';
				uni.showToast({
					title: '已重置',
					icon: 'none',
					duration: 1500
				});
			},
			
			// 验证用户名和密码
			validateLogin(username, password) {
				const registeredUsers = uni.getStorageSync('registeredUsers') || [];
				
				// 查找匹配的用户
				const user = registeredUsers.find(user => 
					user.username === username && user.password === password
				);
				
				return user;
			},

			// 登录逻辑
			handleLogin() {
				// 验证输入
				if (!this.username.trim()) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
					return;
				}

				if (!this.password.trim()) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}

				// 验证用户
				const user = this.validateLogin(this.username, this.password);
				
				if (!user) {
					uni.showToast({
						title: '用户名或密码错误',
						icon: 'none'
					});
					return;
				}

				// 显示加载中
				uni.showLoading({
					title: '登录中...',
					mask: true
				});

				// 模拟登录过程
				setTimeout(() => {
					uni.hideLoading();
					
					// 保存登录信息
					const userInfo = {
						username: user.username,
						avatar: user.avatar || this.avatarUrl,
						email: user.email || '',
						phone: user.phone || '',
						loginTime: new Date().getTime()
					};
					
					// 保存头像到本地
					if (this.avatarUrl && !user.avatar) {
						uni.setStorageSync('userAvatar', this.avatarUrl);
					}
					
					// 保存用户信息
					uni.setStorageSync('userInfo', userInfo);
					uni.setStorageSync('isLogin', true);
					uni.setStorageSync('currentUser', user.username);
					
					// 登录成功提示
					uni.showToast({
						title: '登录成功',
						icon: 'success',
						duration: 1500
					});
					
					// 发送登录成功事件
					uni.$emit('userLoginSuccess', userInfo);
					
					// 返回上一页（个人主页）
					setTimeout(() => {
						uni.navigateBack({
							delta: 1,
							success: () => {
								console.log('返回个人主页成功');
							}
						});
					}, 1500);
				}, 1500);
			}
		}
	};
</script>

<style scoped>
	.login-page {
		width: 100%;
		min-height: 100vh;
		background-color: white;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 40rpx;
		box-sizing: border-box;
	}

	/* 头像上传区域 */
	.avatar-section {
		margin-bottom: 80rpx;
	}

	.avatar-container {
		position: relative;
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;
		overflow: hidden;
		border: 4rpx solid #f0f0f0;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
	}

	.avatar {
		width: 100%;
		height: 100%;
	}

	.upload-hint {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: rgba(0, 0, 0, 0.5);
		padding: 20rpx 0;
		text-align: center;
	}

	.upload-hint text {
		font-size: 24rpx;
		color: white;
	}

	/* 表单区域 */
	.form-section {
		width: 100%;
		max-width: 600rpx;
	}

	/* 输入行 */
	.input-row {
		display: flex;
		align-items: center;
		margin-bottom: 40rpx;
		padding: 20rpx 0;
		border-bottom: 1px solid #e0e0e0;
	}

	.label {
		font-size: 32rpx;
		color: #333;
		width: 140rpx;
		flex-shrink: 0;
		font-weight: 500;
	}

	.input {
		flex: 1;
		font-size: 32rpx;
		color: #333;
		border: none;
		outline: none;
		padding: 10rpx 0;
		font-weight: 400;
	}

	.input::placeholder {
		color: #999;
		font-size: 28rpx;
	}

	/* 按钮行 */
	.button-row {
		display: flex;
		justify-content: space-between;
		gap: 40rpx;
		margin-top: 60rpx;
		margin-bottom: 40rpx;
	}

	.btn {
		flex: 1;
		height: 80rpx;
		border-radius: 12rpx;
		font-size: 32rpx;
		font-weight: 500;
		line-height: 80rpx;
		border: none;
		transition: all 0.3s ease;
	}

	.reset-btn {
		background-color: #f5f5f5;
		color: #666;
	}

	.reset-btn:active {
		background-color: #e0e0e0;
	}

	.login-btn {
		background-color: #ff4d4f;
		color: white;
	}

	.login-btn:active {
		background-color: #ff3333;
	}

	/* 注册按钮行 */
	.register-row {
		display: flex;
		justify-content: center;
		margin-top: 30rpx;
	}

	.register-btn {
		width: 100%;
		height: 80rpx;
		background-color: white;
		color: #ff4d4f;
		border: 2rpx solid #ff4d4f;
		border-radius: 12rpx;
		font-size: 32rpx;
		font-weight: 500;
		line-height: 80rpx;
		transition: all 0.3s ease;
	}

	.register-btn:active {
		background-color: #fff5f5;
	}

	/* 移除按钮默认样式 */
	button::after {
		border: none;
	}
</style>